﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LogoScriptIDE
{
    /// <summary>
    /// Interaction logic for InputboxWindow.xaml
    /// </summary>
    public partial class InputvarWindow : Window
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public InputvarWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取输入的变量名
        /// </summary>
        /// <returns>用户输入的变量名</returns>
        public string getVarname()
        {
            return inputVar;
        }

        /// <summary>
        /// 获取输入状态
        /// </summary>
        /// <returns>-1表示读取出错，0表示正在读取，1表示读取成功</returns>
        public int inputStatus()
        {
            return inputDone;
        }

        /// <summary>
        /// 完成按钮的回调函数
        /// </summary>
        private void ui_inputDone_Click(object sender, RoutedEventArgs e)
        {
            Done();
        }

        /// <summary>
        /// 完成输入
        /// </summary>
        private void Done()
        {
            try
            {
                inputVar = Convert.ToString(ui_inputText.Text);
            }
            catch
            {
                //读取失败
                inputDone = -1;
                Hide();
                return;
            }
            inputDone = 1;
            Hide();
        }

        /// <summary>
        /// 窗口关闭回调函数
        /// 隐藏窗口并取消关闭
        /// </summary>
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }

        private string inputVar;
        private int inputDone; // -1: something wrong   0: not done     1: done

        /// <summary>
        /// 窗口载入完成回调函数
        /// </summary>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            inputDone = 0;
            //讲输入聚焦到输入框
            ui_inputText.Focus();
        }

        /// <summary>
        /// 取消按钮回调函数
        /// </summary>
        private void ui_cancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// 键盘按下回调函数
        /// 回车表示输入完成
        /// </summary>
        private void ui_inputText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                Done();
        }
    }
}
